package com.ch.playandroid.weight

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import kotlin.math.max

class FlowLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {
    private val allViewList = ArrayList<ArrayList<View>>() //所有的子View个数
    private val itemHorizontalSpacing = 30 //子元素水平间距
    private val itemVerticalSpacing = 30 //子元素竖直间距
    private val lineHeightList = ArrayList<Int>()//所有行高

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        allViewList.clear()
        lineHeightList.clear()
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        val heightModel = MeasureSpec.getMode(heightMeasureSpec)
        val maxWidth = widthSize
        var lineWidth = 0 //行宽
        var maxLineWidth = 0 //最大行宽
        var lineHeight = 0 // 行高
        var maxLineHeight = 0 //最大行高
        var lineCount = 0 //总行数
        var itemViewList = ArrayList<View>()
        for (i in 0 until childCount) {
            val childView = getChildAt(i)
            if (childView.visibility != View.GONE) {
                val lp = childView.layoutParams
                val childWidthMeasureSpec = getChildMeasureSpec(
                    widthMeasureSpec,
                    paddingLeft + paddingRight, lp.width
                )
                val childHeightMeasureSpec = getChildMeasureSpec(
                    heightMeasureSpec,
                    paddingTop + paddingBottom, lp.height
                )
                childView.measure(childWidthMeasureSpec, childHeightMeasureSpec)
                //子View测量后，再得到子View的宽高
                val childViewWidth = childView.measuredWidth
                val childViewHeight = childView.measuredHeight
                val mCurItemHorizontalSpacing = if (lineWidth == 0) 0 else itemHorizontalSpacing
                if (lineWidth + mCurItemHorizontalSpacing + childViewWidth <= maxWidth - paddingLeft - paddingRight) //最大宽度
                {
                    lineWidth += mCurItemHorizontalSpacing + childViewWidth
                    lineHeight = max(lineHeight, childViewHeight)
                    itemViewList.add(childView)
                } else {
                    maxLineWidth = max(lineWidth, maxLineWidth)
                    lineCount++
                    maxLineHeight += lineHeight + if (lineCount == 1) 0 else itemVerticalSpacing
                    lineHeightList.add(lineHeight)
                    allViewList.add(itemViewList)

                    lineWidth = childViewWidth
                    lineHeight = childViewHeight
                    itemViewList = ArrayList()
                    itemViewList.add(childView)

                }

            }
            if (i == childCount - 1) {
                maxLineWidth = max(lineWidth, maxLineWidth)
                lineCount++
                maxLineHeight += lineHeight + if (lineCount == 1) 0 else itemVerticalSpacing
                lineHeightList.add(lineHeight)
                allViewList.add(itemViewList)
            }
        }

        maxLineWidth += paddingLeft + paddingRight
        maxLineHeight += paddingTop + paddingBottom
        val measuredWidth = if (widthMode == MeasureSpec.EXACTLY) widthSize else maxLineWidth
        val measuredHeight = if (heightModel == MeasureSpec.EXACTLY) heightSize else maxLineHeight
        setMeasuredDimension(measuredWidth, measuredHeight)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val lineCount = allViewList.size
        var childLeft = paddingLeft //初始x坐标
        var childTop = paddingRight //初始y坐标
        for (i in 0 until lineCount) {
            val lineViews = allViewList[i]
            val lineHeight = lineHeightList[i]
            for (j in 0 until lineViews.size) {
                val childView = lineViews[j]
                val childWidth = childView.measuredWidth
                val childHeight = childView.measuredHeight
                childView.layout(
                    childLeft, childTop, childLeft + childWidth,
                    childTop + childHeight
                )
                childLeft += childWidth + itemHorizontalSpacing
            }

            childTop += lineHeight + itemVerticalSpacing
            childLeft = paddingLeft
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        var mX = 0f
        var mY = 0f
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                mX = event.x
                mY=event.y
            }

            MotionEvent.ACTION_UP ->{

            }
        }
        return super.onTouchEvent(event)
    }
}